﻿$(function () {
    $("#nodes").jstree({
        core: {
            html_titles: false,
            animation: 500,
            initially_open: [],
            initially_load: [],
            load_open: false,
            open_parents: true,
            notify_plugins: true,
            rtl: false,
            strings: { loading: "Loading ...", new_node: "New node" }
        },
        plugins: ["json_data", "themes", "dnd", "contextmenu", "crrm"],
        json_data: {
            ajax: {
                url: "/Sitemap/GetNodes",
                method: "post",
                data: function (n) {
                    return {
                        nodeid: n.attr ? n.attr("id") : 0
                    };
                }
            }
        }

    })
    .bind("move_node.jstree", function (e, data) {
        data.rslt.o.each(function (i) {

            var input = {
                nodeid: $(this).attr("id"),
                parentnodeid: data.rslt.cr === -1 ? 1 : data.rslt.np.attr("id")
            };

            alert("move " + input.nodeid + " to " + input.parentnodeid);

            Ajax({
                url: "/Sitemap/MoveNode",
                data: JSON.stringify(input),
                success: function (r) {
                    if (!r.status) {
                        $.jstree.rollback(data.rlbk);
                    }
                    else {
                        //$(data.rslt.oc).attr("id", r.id);
                        if (data.rslt.cy && $(data.rslt.oc).children("UL").length) {
                            data.inst.refresh(data.inst._get_parent(data.rslt.oc));
                        }
                    }
                }
            });
        });
    })
    .bind("create.jstree", function (e, data) {

        var input = {
            parentnodeid: data.rslt.parent.attr("id"),
            title: data.rslt.name
        };

        alert("create " + input.title + " to " + input.parentnodeid);

        Ajax({
            url: "/Sitemap/CreateNode",
            data: JSON.stringify(input),
            success: function (r) {
                if (r.status) {
                    $(data.rslt.obj).attr("id", r.id);

                    alert($(data.rslt.obj).attr("id"));
                }
                else {
                    $.jstree.rollback(data.rlbk);
                }
            }
        });
    })
    .bind("remove.jstree", function (e, data) {
        data.rslt.obj.each(function () {

            var input = {
                nodeid: this.id
            };

            alert("remove " + input.nodeid);

            Ajax({
                url: "/Sitemap/DeleteNode",
                data: JSON.stringify(input),
                success: function (r) {
                    if (!r.status) {
                        data.inst.refresh();
                    }
                }
            });
        });
    })
    .bind("rename.jstree", function (e, data) {

        var input = {
            nodeid: data.rslt.obj.attr("id"),
            title: data.rslt.new_name
        };

        alert("rename " + input.title + " for " + input.nodeid);

        Ajax({
            url: "/Sitemap/RenameNode",
            data: JSON.stringify(input),
            success: function (r) {
                if (!r.status) {
                    $.jstree.rollback(data.rlbk);
                }
            }
        });
    });
});